package Homework.jdbcWork2;

import jdbc.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2022-05-15:03
 */
public class selectMsg {
    public static Scanner sc = new Scanner(System.in);
    public static final int PAGESIZE = 10;

    public static void main(String[] args) {
        System.out.println("输入姓名:");
        String name = sc.nextLine();
        System.out.println("输入qq号:");
        String qq = sc.nextLine();
        System.out.println("输入手机号:");
        String telephone = sc.nextLine();
        search(name, qq, telephone);
    }

    public static void search(String sname, String qq, String telephone) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String str = "select stuno,sname,qq,telephone from s_student where 1=1 ";
        if (sname != null && sname.equals("")) {
            str += "and sname like '%" + sname + "%' ";
        }
        if (qq != null && qq.equals("")) {
            str += "and qq like '%" + qq + "%' ";
        }
        if (telephone != null && telephone.equals("")) {
            str += "and telephone like '%" + telephone + "%' ";
        }
        str += "limit ?,?";
        try {
            con = DButil.getCon();
            System.out.println("输入页数：");
            int no1 = sc.nextInt();
            ps = con.prepareStatement(str);
            ps.setInt(1, (no1 - 1) * PAGESIZE);
            ps.setInt(2, PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "\t"
                        + rs.getString(2) + "\t"
                        + rs.getString(3) + "\t"
                        + rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(rs, ps, con);
        }
    }

}
